<template>
  <div class="app-container hk-flex-column">
    <header class="hk-flex">
      <div class="hk-flex left">
        <img src="@/assets/images/tj-icon-04.png" alt />
        <div class="total-area">
          <p class="font-size16 total-title">全部用户</p>
          <span class="font-c-blue">{{ countObj.sysUserCount || 0 }}</span>
        </div>
      </div>
      <!-- <div v-if="countObj.DeptTypeCount.length" class="tab-list tab-bg hk-flex">
        <div class="icon hk-flex-column">
          <img :src="require(`@/assets/images/icon1@2x.png`)" alt />
          <p>类型</p>
        </div>
        <div class="hk-flex-column flex-1">
          <div v-for="(i, idx) in countObj.DeptTypeCount.slice(0, 3)" :key="idx" class="sp-flex">
            <span>{{ i.typeName }}</span>
            <i>{{ i.typeCount }}</i>
          </div>
        </div>
      </div> -->
      <div v-if="countObj.secLevelCount.length" class="tab-list tab-bg hk-flex">
        <div class="icon hk-flex-column">
          <img :src="require(`@/assets/images/icon2@2x.png`)" alt />
          <p>涉密等级</p>
        </div>
        <div class="hk-flex-column flex-1">
          <el-row :gutter="60">
            <el-col v-for="(i, idx) in countObj.secLevelCount.slice(0, 4)" :key="idx" :span="12" class="spm-flex">
              <span>{{ i.secLevel }}</span>
              <i>{{ i.levelCount }}</i>
            </el-col>
          </el-row>
        </div>
      </div>
    </header>
    <div class="mt-20">
      <div class="head-container card-tree">
        <el-tree
          ref="tree"
          style="height: 100%; width: 100%"
          :data="deptOptions"
          :props="defaultProps"
          :default-expanded-keys="defaultExpandedKeys"
          :expand-on-click-node="false"
          node-key="id"
          highlight-current
          :render-content="renderContent"
          @node-click="handleNodeClick" />
      </div>
      <div class="hk-flex-column tab-content">
        <div class="hk-flex">
          <search-form
            :data="queryParams"
            :field-list="filterInfo.fieldList"
            :list-type-info="dictList"
            @handleSearch="handleQuery"
            @handleReset="resetQuery" />
          <!-- <el-button
            :class="['btn', showhidden ? 'showhidden' : '']"
            size="mini"
            :icon="!showhidden ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
            @click="showhidden = !showhidden" /> -->
        </div>

        <!-- <el-row v-if="showhidden" class="extra">
          <div>离部时间</div>
          <el-date-picker
            v-model="queryParams.departureDate"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd" />
        </el-row> -->
        <el-row style="margin-top: 20px">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAdd">新增</el-button>
          <el-button size="small" :disabled="!ids.length" @click="handleDelete">删除</el-button>
          <el-button size="small" @click="importTemplate">下载模板</el-button>
          <el-button size="small" @click="handleImport">导入</el-button>
          <el-button size="small" @click="handleExport">导出查询结果</el-button>
        </el-row>

        <hk-table
          :table-data="tableData"
          :table-columns="tableList"
          :selection="true"
          :select-fn="selectFn"
          :show-index="true"
          :total="total"
          :loading="loading"
          :table-height="80"
          :dict-list="dictList"
          operate
          :operate-width="220"
          :query-params="queryParams"
          @handleSelectionChange="handleSelectionChange"
          @changePage="getDataList">
          <template #name="scope">
            <span>{{ scope.row.name }}</span>
            <span v-if="scope.row.nationStatus == 1" class="hk-tag serious">离</span>
          </template>
          <template #fullName="scope">
            <el-tooltip class="item" effect="dark" :content="scope.row.fullName" placement="top">
              <span>{{ scope.row.fullName }}</span>
            </el-tooltip>
          </template>
          <template #nationStatus="scope">
            <img class="symbol" :src="require(`@/assets/images/00${scope.row.nationStatus == 1 ? 3 : 2}.png`)" alt />
            <dict-label :options="dictList.statusList" :value="scope.row.nationStatus" />
          </template>
          <template #secLevel="scope">
            <span
              v-if="scope.row.secLevel"
              class="hk-level"
              :class="
                scope.row.secLevel == 1
                  ? 'safe'
                  : scope.row.secLevel == 3
                  ? 'fairly'
                  : scope.row.secLevel == 4
                  ? 'serious'
                  : ''
              ">
              <dict-label :options="dictList.secLevelList" :value="scope.row.secLevel" />
            </span>
          </template>
          <template #operate="scope">
            <el-button size="mini" type="text" @click="handleDetail(scope.row)">查看</el-button>
            <el-button v-if="scope.row.opUserTag !== '1'" size="mini" type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button v-if="scope.row.opUserTag !== '1'" size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </hk-table>
        <hk-upload
          ref="import_dia"
          :url="url"
          :title="title"
          :accept="accept"
          @submit="
            getDataList()
            getCount()
          " />
      </div>
    </div>
  </div>
</template>

<script>
import { pageList, count, listByCode, deleteItem, orglist } from '@/api/specialProject/systemUser'
import SearchForm from '@/components/HkSearchform/index.vue'
import HkTable from '@/components/Hk-Table/index.vue'
import dictLabel from '@/components/Common/dictLabel.vue'
export default {
  components: {
    SearchForm,
    HkTable,
    dictLabel,
  },
  beforeRouteLeave(to, from, next) {
    console.log(to, '路由')
    if (to.path === '/specialProject/userDetail') {
      const condition = JSON.stringify(this.queryParams)
      sessionStorage.setItem('condition', condition)
    } else {
      sessionStorage.removeItem('condition')
    }
    next()
  },
  data() {
    return {
      queryParams: {
        current: 1,
        size: 10,
      },
      ids: [],
      checkedList: [],
      tableData: [],
      total: 0,
      countObj: {
        DeptTypeCount: [],
        secLevelCount: [],
      },
      showhidden: false,
      // 部门树选项
      deptOptions: undefined,
      // 树结构参数
      defaultProps: {
        children: 'children',
        label: 'name',
      },

      url: window.baseUrl.action + '/sysUser/import',
      title: '导入系统用户',
      accept: '.xlsx',
      loading: false,
      tableList: [
        { label: '人员名称', param: 'name', type: 'slot', width: '200' },
        { label: '人员编号', param: 'userNo' },
        { label: '所属部门', param: 'fullName', width: 200, type: 'slot' },
        { label: '状态', param: 'nationStatus', type: 'slot', list: 'statusList' },
        { label: '涉密等级', param: 'secLevel', type: 'slot', list: 'secLevelList' },
        { label: '在岗类别', param: 'nationType', type: 'dict', list: 'nationTypeList' },
        // { label: '岗位', param: 'post' },
        // { label: 'Ukey账号', param: 'ukeyAccount' },
        // { label: '离部时间', param: 'departureDate' },
      ],
      filterInfo: {
        fieldList: [
          { label: '关键字', type: 'input', field: 'keyword', placeholder: '请输入用户名称', maxlength: 20 },
          { label: '涉密等级', type: 'select', field: 'secLevel', list: 'secLevelList' },
          { label: '在岗类别', type: 'select', field: 'nationType', list: 'nationTypeList' },
          // { label: '是否有Ukey', type: 'select', field: 'isUKey', list: 'whetherList' },
          { label: '状态', type: 'select', field: 'nationStatus', list: 'statusList' },
        ],
      },
      dictList: {
        secLevelList: [],
        nationTypeList: [],
        whetherList: [],
        statusList: [],
      },
      defaultExpandedKeys: [],
    }
  },
  created() {
    var cd = sessionStorage.getItem('condition')
    if (cd) {
      var condition = JSON.parse(cd)
      this.queryParams = condition
    }
  },
  mounted() {
    this.getTypes()
    this.getTree()

    this.getCount()
  },
  methods: {
    renderContent(h, { node, data, store }) {
      return (
        <div class="custom-tree-node">
          <el-tooltip class="item" effect="dark" placement="top">
            <div slot="content">{node.label}</div>
            <p>{node.label}</p>
          </el-tooltip>
        </div>
      )
    },
    selectFn(row, index) {
      return row.opUserTag !== '1'
    },
    //  查询
    handleQuery() {
      this.queryParams.current = 1
      this.getDataList()
    },
    //  重置
    resetQuery() {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getDataList()
    },
    getTree() {
      orglist().then((res) => {
        this.deptOptions = res
        if (this.deptOptions.length > 0) {
          this.defaultExpandedKeys.push(this.deptOptions[0].id)
        }
      })
      this.getDataList()
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getDataList({ orgId: data.id })
    },

    getDataList(val) {
      this.queryParams = { ...this.queryParams, ...val }
      this.loading = true
      pageList(this.queryParams).then((res) => {
        this.loading = false
        this.tableData = res.list
        this.total = res.total
        const condition = JSON.stringify(this.queryParams)
        sessionStorage.setItem('condition', condition)
      })
    },
    getTypes() {
      listByCode('secLevel,nationType,whether,status').then((res) => {
        for (const key in res) {
          this.dictList[key + 'List'] = res[key].map((i) => {
            return {
              label: i.dictValue,
              value: i.dictKey,
            }
          })
        }
      })
    },
    getCount() {
      count().then((res) => {
        this.countObj = res
        this.countObj.secLevelCount = res.secLevelCount.sort((a, b) => {
          return b.levelCount - a.levelCount
        })
      })
    },
    handleSelectionChange(obj) {
      console.log(obj, 'sss')
      // this.checkedList = obj
      this.ids = obj.map((i) => i.id)
    },
    handleDetail(row) {
      this.$router.push({
        path: window.prefix + '/specialProject/userDetail',
        query: { id: row.id, showDetail: true, isYW: row.opUserTag },
      })
    },
    handleAdd() {
      this.$router.push(window.prefix + '/specialProject/userDetail')
    },
    handleEdit(row) {
      this.$router.push(window.prefix + '/specialProject/userDetail?id=' + row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function () {
          return deleteItem(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getDataList()
          this.getCount()
        })
        .catch((error) => {
          console.log(error)
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import_dia.show()
    },
    /** 下载模板操作 */
    importTemplate() {
      this.gDownload(window.baseUrl.action + '/sysUser/importTemplate', {}, `系统用户导入模板.xlsx`)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        window.baseUrl.action + '/sysUser/export',
        {
          ...this.queryParams,
        },
        `系统用户${new Date().getTime()}.xlsx`,
        'daochu'
      )
    },
  },
}
</script>

<style lang="scss" scoped>
#app.dark-theme {
  .app-container header,
  .mt-20,
  // .tab-bg,
  .extra {
    background: transparent !important;
    color: #fff;
  }
  .tab-bg {
    background: #122452 !important;
  }

  .spm-flex {
    i {
      color: #fff;
    }
  }
  .sp-flex i {
    color: #fff;
  }
  .extra div {
    color: #fff;
  }
}
::v-deep .cell > span {
  display: inline-block;
  text-overflow: ellipsis;
  overflow: hidden;
}
img {
  width: 60px;
}
.app-container {
  background: #eef0f5 !important;
  padding: 0;
  header {
    padding: 20px;
    background: #fff;
    height: 160px;
    border-radius: 4px;

    .left {
      width: 20%;
      max-width: 250px;
      img {
        width: 100px;
      }
    }
    .total-area {
      margin-left: 5px;
    }
    .tab-bg {
      background: #f3f5fb;
      padding: 20px;
      border-radius: 4px;
    }
    .icon {
      align-items: center;
      margin-right: 20px;
      p {
        margin: 0;
        margin-top: 10px;
      }
    }
    .sp-flex {
      span {
        max-width: 90%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      i {
        font-style: normal;
        font-size: 18px;
        color: #333333;
        line-height: 24px;
        font-weight: 600;
      }
    }
    .tab-list {
      height: 100%;
      margin-right: 20px;
      flex: 1;
      max-width: 360px;
    }
  }
}
.spm-flex {
  height: 100%;
  padding-right: 10%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  i {
    font-style: normal;
    font-size: 18px;
    color: #333333;
    // line-height: 24px;
    font-weight: 600;
  }
}
.tab-content {
  flex: 1;
  overflow: auto;
  padding: 20px;
  position: relative;
}
.hk-flex {
  flex-wrap: wrap;
}
.extra {
  border-top: 1px solid #4b6eef;
  padding: 15px 10px;
  background: #f3f5fc;
  > div {
    margin-bottom: 10px;
    font-size: 14px;
    color: #606266;
    font-weight: 700;
  }
}
.total-title {
  margin-bottom: 15px;
  margin-top: 6px;
  line-height: 24px;
}
.font-size16 {
  font-size: 16px;
}
.symbol {
  width: 14px;

  margin-right: 10px;
}
.flex-1 {
  width: calc(100% - 80px);
  justify-content: space-between;
  height: 100%;
}
.font-c-blue {
  color: #4b6eef;
  font-size: 36px;
}

.btn {
  margin-left: 10px;
  transform: translateY(16px);
  position: relative;
  &.showhidden {
    &::before {
      content: '';
      position: absolute;
      width: 0;
      height: 0;
      top: 40px;
      border: 6px solid #4b6eef;
      border-top-color: transparent;
      border-left-color: transparent;
      border-right-color: transparent;
    }
  }
}
.mt-20 {
  margin-top: 20px;
  background: #fff;
  display: flex;
  flex: 1;
  overflow: auto;
  .head-container {
    overflow: auto;
    padding: 20px;
    // height: 100%;
    width: 220px;
    border-right: 1px solid rgba(0, 0, 0, 0.08);
  }
}

::v-deep {
  .content {
    display: inline-flex;
    padding-bottom: 0;
  }
  .cell {
    display: flex;
    align-items: center;
  }
}
::v-deep .card-tree {
  height: calc(100vh - 340px);
  overflow: auto;
  .el-tree-node.is-expanded > .el-tree-node__children {
    display: inline;
  }
}
</style>
